package com.dxd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dxd.dao.MessageBoardDao;
import com.dxd.dao.MessageDao;
import com.dxd.dao.MessageTypeDao;
import com.dxd.dao.UserInfoDao;
import com.dxd.entity.ReturnBean;
import com.dxd.entity.UserInfo;
import com.dxd.utils.JSONTools;

/**
 * 公共的servlet
 * 
 * @author pppcar
 *
 */
public class PublicServlet extends HttpServlet {

	protected static final long serialVersionUID = -2054938125977776114L;

	private static final Logger LOG = LoggerFactory.getLogger(PublicServlet.class);

	protected UserInfoDao userInfoDao = new UserInfoDao();
	protected MessageTypeDao messageTypeDao = new MessageTypeDao();
	protected MessageDao messageDao = new MessageDao();
	protected MessageBoardDao messageBoardDao = new MessageBoardDao();
	protected Map<String, Integer> pageMap = new HashMap<String, Integer>();// 分页封装
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		String pageStr = arg0.getParameter("page");// 当前页
		String sizeStr = arg0.getParameter("size");// 每页显示条数
		int page = 1;
		int size = 5;
		if (pageStr != null && !("".equals(pageStr))) {
			page = Integer.parseInt(pageStr);
		}
		if (sizeStr != null && !("".equals(sizeStr))) {
			size = Integer.parseInt(sizeStr);
		}
		pageMap.put("page", page);
		pageMap.put("size", size);
		this.request = arg0;
		this.response = arg1;
		super.service(arg0, arg1);
	}

	// 跳转
	protected void Dispatcher(String url) throws ServletException, IOException {
		request.getRequestDispatcher(url).forward(request, response);
	}

	// 重定向
	protected void Redirect(String url) throws IOException {
		response.sendRedirect(url);
	}

	/**
	 * 返回json数据
	 * 
	 * @param map
	 */
	protected void returnJson(ReturnBean returnBean) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.write(JSONTools.obj2json(returnBean));
	}

	/**
	 * 后端获取登录的用户信息 获取登录的用户信息
	 * 
	 * @return
	 */
	protected UserInfo getUserInfo() {
		UserInfo userInfo = null;
		Object userId = request.getSession().getAttribute("userId");
		if (userId == null) {
			LOG.info("获取登录信息失败，当前状态未登录");
			return userInfo;
		}
		userInfo = userInfoDao.findById(Integer.parseInt(String.valueOf(userId)));
		if (userInfo == null || userInfo.getId() == null) {
			LOG.info("当前用户信息不存在，用户id：{}", userId);
			return null;
		}
		return userInfo;
	}

	/**
	 * 前端获取用户登录信息
	 * 
	 * @return
	 */
	protected UserInfo getUserInfoSwzl() {
		UserInfo userInfo = null;
		Object userId = request.getSession().getAttribute("swzlUserId");
		if (userId == null) {
			LOG.info("获取登录信息失败，当前状态未登录");
			return userInfo;
		}
		userInfo = userInfoDao.findById(Integer.parseInt(String.valueOf(userId)));
		if (userInfo == null || userInfo.getId() == null) {
			LOG.info("当前用户信息不存在，用户id：{}", userId);
			return null;
		}
		return userInfo;
	}

	protected HttpSession getSession() {
		return request.getSession();
	}

}
